package com.forlove.app.qselect.index.presenter;

import android.text.TextUtils;

import com.forlove.app.ForloveApplication;
import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.common.utils.StringUtils;
import com.forlove.app.manager.FriendManager;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.friend.entity.FriendEntity;
import com.forlove.app.qselect.friend.entity.FriendListEntity;
import com.forlove.app.qselect.friend.entity.MineFriendEntity;
import com.forlove.app.qselect.index.service.ICustomCandidateService;
import com.forlove.app.qselect.index.service.impl.CustomCandidateServiceImpl;
import com.forlove.app.qselect.index.view.CustomCandidateView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yinzhiqiang on 2017/11/8.
 */

public class CustomCandidatePresenter extends IBasePresenter {
    private CustomCandidateView candidateView;
    private ICustomCandidateService candidateService;


    public CustomCandidatePresenter(CustomCandidateView candidateView) {
        this.candidateView = candidateView;
        candidateService = new CustomCandidateServiceImpl();
        setService(candidateService);
    }

    public void requestFriends() {//获取后台返回的好友
        candidateService.loadData(ServerManager.ACTION_FRIEND_LIST, new HashMap(), new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                List<MineFriendEntity> friendEntities = new ArrayList<>();
                friendEntities = ParseHelper.getModelList(result, "data.friends", FriendEntity.class);
                List<MineFriendEntity> contacts = ParseHelper.getModelList(result, "data.concats", FriendEntity.class);
                FriendListEntity friendListEntity = ParseHelper.getModel(result, "data", FriendListEntity.class);
                if (friendListEntity != null && friendListEntity.friendEntities.size() > 0) {
                    FriendManager.saveFriendList(ForloveApplication.getInstance(), result);
                }
                candidateView.updateFriendListView(FriendManager.mergeFriends(friendEntities, contacts, -1));
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    candidateView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    candidateView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                }
            }
        });
    }


    public void requestCandidateList() {//获取后台返回的候选人列表
        candidateService.loadData(ServerManager.ACTION_CANDIDATE_LIST, new HashMap(), new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "候选人列表" + result.toString());

                List<MineFriendEntity> candidateEntity =
                        ParseHelper.getModelList(result, "data.concats", FriendEntity.class);
                candidateView.updateCandidate(candidateEntity);

            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    candidateView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    candidateView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                }
            }
        });
    }

    /**
     * 提交设置候选人
     *
     * @param mineFriendEntities
     */
    public synchronized void loadCondidateFriends(List<MineFriendEntity> mineFriendEntities) {
        if (mineFriendEntities == null) {
            return;
        }
        StringBuilder stringBuilder = new StringBuilder();

        for (int i = 0; i < mineFriendEntities.size(); i++) {
            MineFriendEntity mineFriendEntity = mineFriendEntities.get(i);
            if (StringUtils.isNotEmpty(mineFriendEntity.mobile)) {
                stringBuilder.append(mineFriendEntity.mobile);
            } else {
                stringBuilder.append(mineFriendEntity.contactsNumber);
            }
            if (i < mineFriendEntities.size() - 1) {
                stringBuilder.append(",");
            }
        }
        Map param = new HashMap();
        param.put("list", stringBuilder.toString());
        candidateService.loadData(ServerManager.ACTION_ADD_FRIENDS, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                candidateView.updateSetCandidateView(commonEntity);
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    candidateView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    candidateView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                }
            }
        });
    }

}
